﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using BL;
using System.Management.Automation;
using BL.Classes;
using System.IO;

namespace PowerShell_Commander
{
    public partial class MainForm : Form
    {
        public static string command = string.Empty;
        public static string scriptTitle = string.Empty;
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            string script = rtxbScript.Text.Trim();
            try
            {
                Collection<PSObject> objects = BusinessLogic.RunScript(script);
                string result = BusinessLogic.ConvertResultToString(objects);
                rtxbOutput.Text = result;
            }
            catch (Exception ex)
            {
                rtxbOutput.Text = ex.Message;
                rtxbOutput.ForeColor = Color.Red;
            }
           
            //dgvOutput.DataSource = Helper.ConvertTo(objects.ToList());
        }

        private void btnNewWindow_Click(object sender, EventArgs e)
        {
            NewWindow frmNewWindow = new NewWindow();
            frmNewWindow.script = rtxbScript.Text.Trim();
            frmNewWindow.ShowDialog();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        private void lnkOpenCommands_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            List frmList = new List();
            frmList.list = BusinessLogic.GetCommandsList();
            frmList.ShowDialog();
            rtxbScript.Text = command;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            FillScriptList();
            FillSnippetList();
            rtxbScript.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Title frmTitle = new Title();
            DialogResult dr = frmTitle.ShowDialog();
            if (!string.IsNullOrEmpty(scriptTitle.Trim()) && dr == DialogResult.OK)
                  BusinessLogic.SaveScript(scriptTitle.Trim(), rtxbScript.Text.Trim());

            FillScriptList();

        }

        void FillScriptList()
        {
            List<string> scripts = BusinessLogic.GetScriptList();
            lstScripts.Items.Clear();
            lstScripts.Items.AddRange(scripts.ToArray());
        }

        void FillSnippetList()
        {
            List<string> snippets = BusinessLogic.GetSnippetsList();
            cmbSnippets.Items.Clear();
            cmbSnippets.Items.AddRange(snippets.ToArray());
        }

        private void btnStartScript_Click(object sender, EventArgs e)
        {
            string fileName = lstScripts.Items[lstScripts.SelectedIndex].ToString();
            string result = BusinessLogic.RunSavedScript(fileName);
            rtxbOutput.Text = result;
            
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            Snippet snippet = new Snippet();
            snippet.IsNew = true;
            snippet.ShowDialog();
            FillSnippetList();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            rtxbScript.Focus();
            rtxbScript.SelectAll();
            rtxbScript.Copy();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            rtxbScript.Text = Clipboard.GetText();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you shure?", "Confirm", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;

            string name = string.Empty;
            if (lstScripts.SelectedIndex < 0)
                return;

            name = lstScripts.Items[lstScripts.SelectedIndex].ToString();

            if (string.IsNullOrEmpty(name))
                return;

            if (!BusinessLogic.DeleteScript(name))
            {
                MessageBox.Show("Delete faild", "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            FillScriptList();

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            rtxbScript.Text = string.Empty;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            string name = cmbSnippets.SelectedItem.ToString();
            string snippet = BusinessLogic.GetSnippet(name);
            rtxbScript.Text += snippet;
        }

        private void insertCommandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lnkOpenCommands_LinkClicked(sender, null);
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            rtxbScript.Focus();
            rtxbScript.SelectAll();
        }

        private void runToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnRun_Click(sender, null);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            rtxbScript.Copy();
        }

        private void btnDeleteSnippet_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you shure?", "Confirm", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;

            string name = string.Empty;
            if (cmbSnippets.SelectedIndex < 0)
                return;

            name = cmbSnippets.Items[cmbSnippets.SelectedIndex].ToString();

            if (string.IsNullOrEmpty(name))
                return;

            if (!BusinessLogic.DeleteSnippet(name))
            {
                MessageBox.Show("Delete faild", "Info", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            FillSnippetList();
        }

        private void btnEditSnippet_Click(object sender, EventArgs e)
        {
            string name = string.Empty;
            if (cmbSnippets.SelectedIndex < 0)
                return;

            name = cmbSnippets.Items[cmbSnippets.SelectedIndex].ToString();

            Snippet snippet = new Snippet();
            snippet.name = name;
            snippet.IsNew = false;
            snippet.ShowDialog();

            FillSnippetList();

        }

        private void customizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SystemInfo systemInfo = new SystemInfo();
            systemInfo.ShowDialog();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you shure?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                this.Close();
        }

        private void btnClearOutput_Click(object sender, EventArgs e)
        {
            rtxbOutput.Text = string.Empty;
            rtxbOutput.ForeColor = Color.Black;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnClose_Click(sender, null);
        }

        private void btnSaveOutput_Click(object sender, EventArgs e)
        {
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(sfd.FileName,rtxbOutput.Text.Trim());
            }
        }
        
    }
}
